package com.oascarmovie.helper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.json.JSONException;
import org.json.JSONObject;

import com.oascarmovie.Constants;

public class HttpSender {

  private JSONObject jsonObject = null;
 
  public static JSONObject send(String host, String url) {
    JSONObject jsonObj = null;
    HttpClient httpclient = null;
    HttpGet httppost = null;
    HttpResponse response = null;
    try {

      httpclient = new HttpClient();
      httpclient.getHostConfiguration().setHost(host, 80);
      httpclient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");

      GetMethod getMethod = new GetMethod(url);
      httpclient.executeMethod(getMethod);
      httpclient.executeMethod(getMethod);
      String html = getMethod.getResponseBodyAsString();
      jsonObj = new JSONObject(html);

    }
    catch (ClientProtocolException e) {
      e.printStackTrace();
    }
    catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    catch(JSONException e) {
      e.printStackTrace();
    }

    finally {

    }
    
    return jsonObj;
  }
  
  public JSONObject getResult() {
    return this.jsonObject;
  }
  


  /*
   * json��ݸ�ʽ�������Լ���Ϊ���֣�
   * 
   * һ������ͨ�ģ�һ���Ǵ���������ʽ�ģ�
   * 
   * 
   * 
   * 
   * ��ͨ��ʽ�ģ� �������˷��ص�json��ݸ�ʽ���£�
   * 
   * {"userbean":{"Uid":"100196","Showname":"\u75af\u72c2\u7684\u7334\u5b50","Avtar"
   * :null,"State":1}}
   * 
   * �����������£�
   * 
   * // TODO ״̬���� 500 200 int res = 0; res =
   * httpClient.execute(httpPost).getStatusLine().getStatusCode(); if (res ==
   * 200) {
   * 
   * ��������Ϊ200ʱ�������� �õ��������˷���json��ݣ���������
   * 
   * HttpResponse httpResponse = httpClient.execute(httpPost); StringBuilder
   * builder = new StringBuilder(); BufferedReader bufferedReader2 = new
   * BufferedReader( new
   * InputStreamReader(httpResponse.getEntity().getContent())); String str2 =
   * ""; for (String s = bufferedReader2.readLine(); s != null; s =
   * bufferedReader2 .readLine()) { builder.append(s); } Log.i("cat", ">>>>>>" +
   * builder.toString());
   * 
   * JSONObject jsonObject = new JSONObject(builder.toString())
   * .getJSONObject("userbean");
   * 
   * String Uid; String Showname; String Avtar; String State;
   * 
   * Uid = jsonObject.getString("Uid"); Showname =
   * jsonObject.getString("Showname"); Avtar = jsonObject.getString("Avtar");
   * State = jsonObject.getString("State"); ��������ʽ�ģ� �������˷��ص���ݸ�ʽΪ��
   * 
   * {"calendar": {"calendarlist": [
   * {"calendar_id":"1705","title":"(\u4eb2\u5b50)ddssd"
   * ,"category_name":"\u9ed8\u8ba4\u5206\u7c7b"
   * ,"showtime":"1288927800","endshowtime":"1288931400","allDay":false},
   * {"calendar_id"
   * :"1706","title":"(\u65c5\u884c)","category_name":"\u9ed8\u8ba4\u5206\u7c7b"
   * ,"showtime":"1288933200","endshowtime":"1288936800","allDay":false} ] } }
   * 
   * �����������£�
   * 
   * // TODO ״̬���� 500 200 int res = 0; res =
   * httpClient.execute(httpPost).getStatusLine().getStatusCode(); if (res ==
   * 200) {
   * 
   * ��������Ϊ200ʱ�������� �õ��������˷���json��ݣ���������
   * 
   * HttpResponse httpResponse = httpClient.execute(httpPost); StringBuilder
   * builder = new StringBuilder(); BufferedReader bufferedReader2 = new
   * BufferedReader( new
   * InputStreamReader(httpResponse.getEntity().getContent())); String str2 =
   * ""; for (String s = bufferedReader2.readLine(); s != null; s =
   * bufferedReader2 .readLine()) { builder.append(s); } Log.i("cat", ">>>>>>" +
   * builder.toString());
   *//**
   * ������Ҫ�����������ش���json��ʽ��ݣ�
   */
  /*
   * JSONObject jsonObject = new JSONObject(builder.toString())
   * .getJSONObject("calendar"); JSONArray jsonArray =
   * jsonObject.getJSONArray("calendarlist"); for(int
   * i=0;i<jsonArray.length();i++){ JSONObject jsonObject2 =
   * (JSONObject)jsonArray.opt(i); CalendarInfo calendarInfo = new
   * CalendarInfo();
   * calendarInfo.setCalendar_id(jsonObject2.getString("calendar_id"));
   * calendarInfo.setTitle(jsonObject2.getString("title"));
   * calendarInfo.setCategory_name(jsonObject2.getString("category_name"));
   * calendarInfo.setShowtime(jsonObject2.getString("showtime"));
   * calendarInfo.setEndtime(jsonObject2.getString("endshowtime"));
   * calendarInfo.setAllDay(jsonObject2.getBoolean("allDay"));
   * calendarInfos.add(calendarInfo); }
   */
}
